<?php
class Db 
{ 
function findBySql($sql) 
{ 
    $array=array();
    $array=$sql;
    return $array; 
    
} 
function toExcel($sql,$mapping,$fileName) 
{  
  
  header("Content-Type: application/vnd.ms-execl");
  header("Content-Disposition: attachment; filename=".$fileName.".xls");
  header("Pragma: no-cache");
  header("Expires: 0");
  echo'<html xmlns:o="urn:schemas-microsoft-com:office:office" 
       xmlns:x="urn:schemas-microsoft-com:office:excel" 
       xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]"> 
       <head> 
       <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT"> 
       <meta http-equiv=Content-Type content="text/html; charset=utf-8"> 
       <!--[if gte mso 9]><xml> 
       <x:ExcelWorkbook> 
       <x:ExcelWorksheets> 
               <x:ExcelWorksheet> 
               <x:Name></x:Name> 
               <x:WorksheetOptions> 
                       <x:DisplayGridlines/> 
               </x:WorksheetOptions> 
               </x:ExcelWorksheet> 
       </x:ExcelWorksheets> 
       </x:ExcelWorkbook> 
       </xml><![endif]--> 
       </head> 
    <body link=blue vlink=purple leftmargin=0 topmargin=0>';  
    echo'<table width="100%" algin="left" border="0" cellspacing="0" cellpadding="0">'; 
       echo'<tr>'; 
    if(is_array($mapping)) 
    { 
      foreach($mapping as $key=>$val) 
   echo'<td>'.$val.'</td>'; 
    } 
    echo'</tr>'; 
    $results=$this->findBySql($sql); 
    foreach($results as $result) 
    { 
      echo'<tr>'; 
      foreach($result as $key=>$val) 
   echo'<td >'.$val.'</td>'; 
   echo'</tr>'; 
    } 
    echo'</table>'; 
    echo'</body>'; 
    echo'</html>';                     
}

} 
?>